package com.infomanage.service.tech;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.TechAccidentDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.TechAccident;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.FileUpload;
import com.infomanage.util.StringUtil;
import com.infomanage.util.smartupload.SmartRequest;
import com.infomanage.util.smartupload.SmartUpload;
import com.infomanage.vo.TechAccidentItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class TechAccidentService {
	
	public static TechAccidentItem loadTechAccident(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String positionId = request.getParameter("itemId");
			if(StringUtils.isEmpty(positionId)) return null;
			TechAccidentDAO dao = new TechAccidentDAO();
			return dao.find(Integer.parseInt(positionId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<TechAccidentItem> loadTechAccidentList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			TechAccidentDAO dao = new TechAccidentDAO();
			List<TechAccidentItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveTechAccident(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			TechAccident bean = new TechAccident();
			bean.setAccidentDeptName(request.getParameter("accidentDeptName"));
			bean.setAccidentDetail(request.getParameter("accidentDetail"));
			bean.setAccidentExperience(request.getParameter("accidentExperience"));
			bean.setAccidentPersonNum(Integer.parseInt(request.getParameter("accidentPersonNum")));
			bean.setAccidentPlace(request.getParameter("accidentPlace"));
			bean.setAccidentProperty(request.getParameter("accidentProperty"));
			bean.setActionPersonNum(Integer.parseInt(request.getParameter("actionPersonNum")));
			bean.setActionTime(request.getParameter("actionTime"));
			bean.setActionTotalTime(request.getParameter("actionTotalTime"));
			bean.setCallTime(request.getParameter("callTime"));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDiePersonNames(request.getParameter("diePersonNames"));
			bean.setDiePersonNum(Integer.parseInt(request.getParameter("diePersonNum")));
			bean.setHighTempWorkTime(request.getParameter("highTempWorkTime"));
			bean.setHxqTime(request.getParameter("hxqTime"));
			bean.setInjuredPersonNames(request.getParameter("injuredPersonNames"));
			bean.setInjuredPersonNum(Integer.parseInt(request.getParameter("injuredPersonNum")));
			bean.setInputerId(Integer.parseInt(request.getParameter("inputerId")));
			bean.setOtherContent(request.getParameter("otherContent"));
			bean.setOutDeadbodyNum(Integer.parseInt(request.getParameter("outDeadbodyNum")));
			bean.setRescueCommanderName(request.getParameter("rescueCommanderName"));
			bean.setRescueDirectorName(request.getParameter("rescueDirectorName"));
			bean.setRescueTime(request.getParameter("rescueTime"));
			bean.setRestoreRoadwayTime(request.getParameter("restoreRoadwayTime"));
			bean.setReturnTime(request.getParameter("returnTime"));
			bean.setSaveMoney(request.getParameter("saveMoney"));
			bean.setSchematic(request.getParameter("schematic"));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setTreatDeptName(request.getParameter("treatDeptName"));
			TechAccidentDAO dao = new TechAccidentDAO();
			dao.add(bean);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("新增失败！");
		}
		return ret;
	}
	
	
	public static ReturnObject modifyTechAccident(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			TechAccidentDAO dao = new TechAccidentDAO();
			TechAccident bean = dao.find(TechAccident.class, itemId);
			bean.setAccidentDeptName(request.getParameter("accidentDeptName"));
			bean.setAccidentDetail(request.getParameter("accidentDetail"));
			bean.setAccidentExperience(request.getParameter("accidentExperience"));
			bean.setAccidentPersonNum(Integer.parseInt(request.getParameter("accidentPersonNum")));
			bean.setAccidentPlace(request.getParameter("accidentPlace"));
			bean.setAccidentProperty(request.getParameter("accidentProperty"));
			bean.setActionPersonNum(Integer.parseInt(request.getParameter("actionPersonNum")));
			bean.setActionTime(request.getParameter("actionTime"));
			bean.setActionTotalTime(request.getParameter("actionTotalTime"));
			bean.setCallTime(request.getParameter("callTime"));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDiePersonNames(request.getParameter("diePersonNames"));
			bean.setDiePersonNum(Integer.parseInt(request.getParameter("diePersonNum")));
			bean.setHighTempWorkTime(request.getParameter("highTempWorkTime"));
			bean.setHxqTime(request.getParameter("hxqTime"));
			bean.setInjuredPersonNames(request.getParameter("injuredPersonNames"));
			bean.setInjuredPersonNum(Integer.parseInt(request.getParameter("injuredPersonNum")));
			bean.setInputerId(Integer.parseInt(request.getParameter("inputerId")));
			bean.setOtherContent(request.getParameter("otherContent"));
			bean.setOutDeadbodyNum(Integer.parseInt(request.getParameter("outDeadbodyNum")));
			bean.setRescueCommanderName(request.getParameter("rescueCommanderName"));
			bean.setRescueDirectorName(request.getParameter("rescueDirectorName"));
			bean.setRescueTime(request.getParameter("rescueTime"));
			bean.setRestoreRoadwayTime(request.getParameter("restoreRoadwayTime"));
			bean.setReturnTime(request.getParameter("returnTime"));
			bean.setSaveMoney(request.getParameter("saveMoney"));
			bean.setSchematic(request.getParameter("schematic"));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setTreatDeptName(request.getParameter("treatDeptName"));
			dao.modify(bean);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("修改失败！");
		}
		return ret;
	}
	
	public static ReturnObject removeTechAccident(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String positionId = request.getParameter("itemId");
			TechAccidentDAO dao = new TechAccidentDAO();
			dao.remove(TechAccident.class, Integer.parseInt(positionId));
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static List<TechAccidentItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			TechAccidentDAO dao = new TechAccidentDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<TechAccidentItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
			request.setCharacterEncoding("UTF-8");
			String deptId = request.getParameter("deptId");
			String timestamp = request.getParameter("timestamp");
			SearchBean searchBean = new SearchBean();
			if(!StringUtil.isEmpty(deptId)){
				FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
				searchBean.add(field);
			}
			if(!StringUtil.isEmpty(timestamp)){
				FieldBean field = new FieldBean("timestamp", timestamp);
				searchBean.add(field);
			}
			
			//设置权限
			User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
			if(user != null){
				AuthorityBean authority = new AuthorityBean();
				authority.setRole(RoleType.valueOf(user.getRoleId()));
				authority.setDeptId(user.getAssociatedDept());
				searchBean.setAuthority(authority);
			}
		return searchBean;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			TechAccidentDAO dao = new TechAccidentDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	public static ReturnObject removeBatchTechAccident(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			TechAccidentDAO dao = new TechAccidentDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(TechAccident.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static ReturnObject uploadLegend(HttpServletRequest httpRequest, HttpServletResponse response, PageContext pageContext){
		ReturnObject retObj = new ReturnObject();
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			String actionType = httpRequest.getParameter("actionType");
			if (actionType == null) {
				FileUpload fileUpload = new FileUpload("jpg,gif,png,bmp,dwg");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				String[] fileNames = smartUpload.getFileNames();
				if (fileNames != null && fileNames.length > 0) {
					String dir = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
					retObj.SetErrored(false);
					retObj.setErrorMessage(dir);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}
}
